package com.zahidq.nio;

import java.net.StandardSocketOptions;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * User: zahid
 * Date: 17/03/13
 *
 */
public class TcpConfig {

    private final String hostname;
    private final int port;
    private final boolean useDirectByteBuffers;
    private final boolean useSyncNio;

    // TODO think of a better way of encapsulating this
//    private final Map<StandardSocketOptions,Object> socketOptions;
    private final int sendByteBufferSize;
    private final int receiveByteBufferSize;

    public TcpConfig(String hostname, int port, boolean useDirectByteBuffers, boolean useSyncNio,
//                     Map<StandardSocketOptions, Object> socketOptions,
                     int sendByteBufferSize, int receiveByteBufferSize) {
        this.hostname = hostname;
        this.port = port;
        this.useDirectByteBuffers = useDirectByteBuffers;
        this.useSyncNio = useSyncNio;
//        this.socketOptions = socketOptions;
        this.sendByteBufferSize = sendByteBufferSize;
        this.receiveByteBufferSize = receiveByteBufferSize;
    }

    @Override
    public String toString() {
        return "TcpConfig{" +
                "hostname='" + hostname + '\'' +
                ", port=" + port +
                ", useDirectByteBuffers=" + useDirectByteBuffers +
                ", useSyncNio=" + useSyncNio +
//                ", socketOptions=" + socketOptions +
                ", sendByteBufferSize=" + sendByteBufferSize +
                ", receiveByteBufferSize=" + receiveByteBufferSize +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        TcpConfig tcpConfig = (TcpConfig) o;

        if (port != tcpConfig.port) return false;
        if (sendByteBufferSize != tcpConfig.sendByteBufferSize) return false;
        if (receiveByteBufferSize != receiveByteBufferSize) return false;
        if (useDirectByteBuffers != tcpConfig.useDirectByteBuffers) return false;
        if (useSyncNio != tcpConfig.useSyncNio) return false;
        if (hostname != null ? !hostname.equals(tcpConfig.hostname) : tcpConfig.hostname != null) return false;
//        if (socketOptions != null ? !socketOptions.equals(tcpConfig.socketOptions) : tcpConfig.socketOptions != null)
//            return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = hostname != null ? hostname.hashCode() : 0;
        result = 31 * result + port;
        result = 31 * result + sendByteBufferSize;
        result = 31 * result + receiveByteBufferSize;
        result = 31 * result + (useDirectByteBuffers ? 1 : 0);
        result = 31 * result + (useSyncNio ? 1 : 0);
//        result = 31 * result + (socketOptions != null ? socketOptions.hashCode() : 0);
        return result;
    }

    public int getSendByteBufferSize() {
        return sendByteBufferSize;
    }

    public int getReceiveByteBufferSize() {
        return receiveByteBufferSize;
    }

    public String getHostname() {

        return hostname;
    }

    public int getPort() {
        return port;
    }

    public boolean isUseDirectByteBuffers() {
        return useDirectByteBuffers;
    }

    public boolean isUseSyncNio() {
        return useSyncNio;
    }

//    public Map<StandardSocketOptions, Object> getSocketOptions() {
//        return socketOptions;
//    }
}
